<?php

namespace app\api\controller;

use app\home\model\system\SystemAttachment;
use app\models\store\StoreCouponIssue;
use app\models\store\StoreService;
use app\models\user\UserBill;
use app\models\user\WechatUser;
use app\Request;
use services\GroupDataService;
use app\models\system\SystemStore;
use services\SystemConfigService;
use think\facade\Db;
use utils\Json;

/**
 * 公共的接口
 * Class PublicController
 * @package app\api\controller
 */
class PublicController extends BaseController
{
    /**
     * 首页的数据
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(Request $request)
    {
        $banner = GroupDataService::getData('routine_home_banner') ?: [];//TODO 首页banner图
        $couponList = StoreCouponIssue::getIssueCouponList($request->uid(), 3);
        $subscribe = WechatUser::where('uid', $request->uid() ?? 0)->value('subscribe') ? true : false;
        $storeList = SystemStore::getList();
        // 获取所有的卡券
        $storeCouponList = Db::name("store_coupon")->where("wxapp_id", $this->wxapp_id)->where("is_del", 0)->select();
        $newGoodsBananr = sys_config('new_goods_bananr');
        return app('json')->successful(compact('banner', 'storeList', 'couponList', 'subscribe', 'newGoodsBananr', "storeCouponList"));
    }

    /**
     * 获取分享配置
     * @return mixed
     */
    public function share()
    {
        $data["img"] = sys_config("wechat_share_img");
        if (strstr($data['img'], 'http') === false) $data['img'] = sys_config('site_url') . $data['img'];
        $data['img'] = str_replace('\\', '/', $data['img']);
        $data['title'] = sys_config('wechat_share_title');
        $data['synopsis'] = sys_config('wechat_share_synopsis');
        return app("json")->successful(compact("data"));
    }

    public function menu_user(Request $request)
    {
        $menusInfo = sys_data('routine_my_menus') ?? [];
        $user = $request->user();
    }

    /**
     * 上传图片
     * @param Request $request
     */
    public function upload_image(Request $request)
    {
        $base64 = $request->param("file");
        $wxapp_id = $request->param("wxapp_id");
        $res = Db::name("opinion_image")->insertGetId([
            'wxapp_id' => $wxapp_id,
            'url' => $base64,
            'add_time' => time()
        ]);
        $data = ['img_id' => $res, 'file' => $base64];
        if ($res) return app("json")->successful(compact('data'));
        return app("json")->fail("图片上传失败");
    }

    /**
     * 删除图片
     * @param Request $request
     * @param $file_id
     * @return Json
     */
    public function remove_img(Request $request, $file_id = 0)
    {
        if (!$file_id) return app("json")->fail("图片不存在");
        if (Db::name("opinion_image")->where('id', $file_id)->delete()) {
            return app("json")->successful("删除成功");
        }
        return app("json")->successful("删除失败");
    }

    /**
     * 获取设备的信息
     * @param Request $request
     */
    public function washInfo(Request $request)
    {
        $id = $request->param("device_id");
        $store_id = $request->param("store_id");
        $data = Db::name("store_device")->where("id", $id)->field("id, device_name, device_no")->find();
        $store_info = Db::name("system_store")->where('id', $store_id)->field("id, name, introduction, phone, address, detailed_address")->find();
        return app("json")->successful(compact("data", "store_info"));
    }

    /**
     * 获取用户的协议
     * @param Request $request
     */
    public function userAgreement(Request $request)
    {
        // 获取用户的协议
        $msg = SystemConfigService::get("user_agreement");
        return app("json")->successful(compact("msg"));
    }
}